package br.com.Servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.Dao.EnderecoDao;
import br.com.Dao.PaginaDao;
import br.com.Dao.UsuarioClienteDao;
import br.com.Dao.UsuarioComercianteDao;
import br.com.Modelo.Endereco;
import br.com.Modelo.Pagina;
import br.com.Modelo.UsuarioCliente;
import br.com.Modelo.UsuarioComerciante;

/**
 * Servlet implementation class CadastroUsuarioComerciante
 */
@WebServlet("/cad_comerciante")
public class CadastroUsuarioComerciante extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadastroUsuarioComerciante() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		UsuarioComerciante comerciante = new UsuarioComerciante();
		comerciante.setLogin(request.getParameter("login_empresa"));	
		comerciante.setSenha(request.getParameter("senha_empresa"));
		comerciante.setNome(request.getParameter("nome_empresa"));
		comerciante.setCnpj(request.getParameter("cnpj"));
		comerciante.setRamo(request.getParameter("ramo_empresa"));
	    String perfil_pagina = request.getParameter("perfil_pagina"); 
		
		
	  
    int id_pagina=0;
   	UsuarioComercianteDao dao = new UsuarioComercianteDao();
   	PaginaDao PageDao = new PaginaDao();
   	
   	Pagina page = new Pagina();
   	page.setPagina(perfil_pagina);
   	page.setImgPagina("");
   
       
		
        if(comerciante.getLogin()==""||
          comerciante.getSenha()=="" ||
          comerciante.getNome() == "" || 
          comerciante.getCnpj() == ""||         
          comerciante.getRamo()==""){
        	
        	RequestDispatcher rd = request
    			    .getRequestDispatcher("/cadastro.jsp?check-cad=Campos Invalidos!!");
        	//response.sendRedirect("cadastro.jsp?check-cad=Campos Invalidos!!");
        	
        	try {
			    rd.forward(request,response);
			} catch (ServletException e) {
			    // TODO Auto-generated catch block
			    e.printStackTrace();
			} catch (IOException e) {
			    // TODO Auto-generated catch block
			    e.printStackTrace();
			}
        	
        	
        	
        }else if(dao.verificaUsuario(comerciante.getLogin(),comerciante.getCnpj())==1){
				
				RequestDispatcher rd = request
	    			    .getRequestDispatcher("/cadastro.jsp?check-cad=Usuario e Email já Cadastro!!");
	        	//response.sendRedirect("cadastro.jsp?check-cad=Campos Invalidos!!");
	        	
	        	try {
				    rd.forward(request,response);
				} catch (ServletException e) {
				    // TODO Auto-generated catch block
				    e.printStackTrace();
				} catch (IOException e) {
				    // TODO Auto-generated catch block
				    e.printStackTrace();
				}
	        	
	        	
		}else{
		
		
		PageDao.adiciona(page);	
		int idFirstPage = PageDao.getFirstIdPage();
		Endereco endereco = new Endereco(idFirstPage);
		EnderecoDao daoEnd = new EnderecoDao();
		daoEnd.adiciona(endereco);
		comerciante.setIdPagina(idFirstPage);
		
		dao.adiciona(comerciante);
		
		//dao.adiciona(user);
	   response.sendRedirect("/plataforma_marketing/index.jsp");
		//response.sendRedirect("/cadastro.jsp?check-cad=Campos Invalidos!!");		
		
		}
	
	}

}
